unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, OleCtrls, SHDocVw, StdCtrls;

type
  TForm1 = class(TForm)
    NameOfFile: TEdit;
    Button1: TButton;
    OpenDialog: TOpenDialog;
    res: TMemo;
    Start: TButton;
    procedure Button1Click(Sender: TObject);
    procedure StartClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  PElement = ^TElement;
  TElement = record
    Value: Integer;
    Next: PElement;
  end;

var
  Form1: TForm1;


implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  if OpenDialog.Execute then
    NameOfFile.Text := OpenDialog.FileName;
end;

Procedure AddToEnd( Head: PElement; num: Integer );
var
  current: PElement;
begin
  current := Head;
  while current^.Next <> nil do
    current := Current^.Next;
  System.New( current^.next );
  current^.Next^.Next := nil;
  current^.Next^.Value := num;
end;

procedure PrintList( head: PElement );
var
  current: PElement;
begin
  Current := head;
  while current <> nil do begin
    Form1.res.Lines.Add( IntToStr( current^.Value ) );
    current := current^.Next;
  end;
end;

procedure TForm1.StartClick(Sender: TObject);
var
  Input: TextFile;
  i, temp: integer;
  Head, current: PElement;
begin
  System.New( Head );
  Head^.Next := nil;
  AssignFile( input, NameOfFile.Text );
  Reset( input );
  Readln( input, temp );
  Head^.Value := temp;
  While not EOF( input ) do begin
    Readln( input, temp );
    AddToEnd( Head, temp );
  end;

  Form1.res.Lines.Add( '---------------' );
  PrintList( head );

  i := 1;
  current := head;
  while current <> nil do
  begin
    inc( current^.Value, i );
    current := current^.Next;
    inc( i );
  end;

  Form1.res.Lines.Add( '---------------' );
  PrintList( head );
end;

end.
